package com.yellowpage.nsp.action;
  
import java.io.IOException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.yellowpage.white.common.Configuration;
import com.yellowpage.white.common.Constants;
import com.yellowpage.white.struts.BaseAction;
import com.yellowpage.white.struts.Module;
import com.yellowpage.white.struts.OperationType;
import com.yellowpage.white.util.ReadLog;
import com.yellowpage.white.util.Util4Java;
import com.yellowpage.nsp.data.NspLogSum;
import com.yellowpage.nsp.service.NspLogSumService;
import com.yellowpage.nsp.service.NspLoggerService;

public class NspLogSumAction extends BaseAction {

    public String RESULT_QUERY = "query";
    public String RESULT_ADD = "add";
	public String RESULT_EDIT = "edit";
	
	public String RESULT_TELLDAY = "tellDay";
	
	public String RESULT_TELLMONTH = "tellMonth";
	
	private Configuration configuration;
	private NspLoggerService nspLoggerService;
	
	public void setConfiguration(Configuration configuration) {
		this.configuration = configuration;
	}
	
	private List<NspLogSum> nspLogSumList = new ArrayList<NspLogSum>();
	private List<Map> resultList = new ArrayList<Map>();
	
	private NspLogSum nspLogSum;
	private NspLogSumService nspLogSumService;
	private ReadLog readlog;
	
	private String selectedId;	
	private String[] selectedIds;	
	private String message;
	
		private String query_logSumId;
		private String query_serverId;
		private String query_logFileName;
		private String query_logTime;
		private String query_requestType;
		private String query_requestContent;
		private String query_responseContent;
		private String query_createUser;
		private String query_createTime;
		private String query_note;
		private String query_responseCode;
		private String query_count;
		private String query_status;
		
		
		private String query_requestTime;
		private String query_requestTime_end;
		
		
		private String query_yearStart;
		private String query_monthStart;
		private String query_yearEnd;
		private String query_monthEnd;
		

		
		private Map requestTypeMap = new HashMap();
		
		private Map yearMap = new HashMap();
		private Map monthMap = new HashMap();
		
	public void prepare() {
		super.prepare();
		requestTypeMap=nspLoggerService.getrequestTypeMap();
		
		for(int p=12;p>=1;p--){
			monthMap.put(p, p);
		}
		
		for(int d=11;d>0;d--){
			int t = 2014+d;
			yearMap.put(d, t);
		}
		
		if (this.selectedId != null && !this.selectedId.equals("")) {
			this.nspLogSum = nspLogSumService.getNspLogSum(Long.parseLong(selectedId));
		}
	}
	
	public String queryByPage() {		
		initPageRender();
		
		Map queryMap = new HashMap();	
			    queryMap.put("logSumId", query_logSumId);
	    	    queryMap.put("serverId", query_serverId);
	    	    queryMap.put("logFileName", query_logFileName);
	    	    queryMap.put("logTime", query_logTime);
	    	    queryMap.put("requestType", query_requestType);
	    	    queryMap.put("requestContent", query_requestContent);
	    	    queryMap.put("responseContent", query_responseContent);
	    	    queryMap.put("createUser", query_createUser);
	    	    queryMap.put("createTime", query_createTime);
	    	    queryMap.put("note", query_note);
	    	    queryMap.put("responseCode", query_responseCode);
	    	    queryMap.put("count", query_count);
	    	    queryMap.put("status", query_status);
	    	    	
		resultList = nspLogSumService.queryNspLogSumByPage(queryMap, pageRender);
		
		setPageRenderToRequest();

		return RESULT_QUERY;
	}
	
	/*电话号码日统计*/
	
	public String nspLogerSumTellDay(){
		
		return RESULT_TELLDAY;
	}
	
	public String nspLogerSumTellDayQuery(){
		
		Map queryMap = new HashMap();	
	    queryMap.put("requestType", query_requestType);
	    queryMap.put("requestTimeStart", query_requestTime);
	    queryMap.put("requestTimeEnd", query_requestTime_end);
	    
	    resultList = nspLogSumService.queryNspLogSumTellDay(queryMap);
	    
	    return RESULT_TELLDAY;
		
	}
	   
	/*电话号码月统计*/
	
	public String nspLogerSumTellMonth(){
		
		return RESULT_TELLMONTH;
	}
	
	
	public String nspLogerSumTellMonthQuery(){		

		if(query_yearStart!=null && !query_yearStart.isEmpty()){
			query_yearStart = Integer.parseInt(query_yearStart)+2014+"";
		}
		if(query_yearEnd!=null && !query_yearEnd.isEmpty()){
			query_yearEnd = Integer.parseInt(query_yearEnd)+2014+"";
		}
		if(!query_monthStart.isEmpty()){
			if(Integer.parseInt(query_monthStart)<10){
				query_monthStart = "0"+query_monthStart;
			}
		}
		if(!query_monthEnd.isEmpty()){
			if(Integer.parseInt(query_monthEnd)<10){
				query_monthEnd = "0"+query_monthEnd;
			}
		}
		Map queryMap = new HashMap();	
	    queryMap.put("requestType", query_requestType);
	    queryMap.put("yearStart", query_yearStart);
	    queryMap.put("monthStart", query_monthStart);
	    queryMap.put("yearEnd", query_yearEnd);
	    queryMap.put("monthEnd", query_monthEnd);
	    
	    resultList = nspLogSumService.queryNspLogSumTellMonth(queryMap);
		
		return RESULT_TELLMONTH;
	}
	
	
	
	
	
	
	
	
	
	
	public String toAdd() {
		
		return RESULT_ADD;
	}
	
	public String save() {
		
		nspLogSumService.saveNspLogSum(nspLogSum);	
		message = "新增成功!";
		return RESULT_ADD;
	}
	
	/*
	 * 传入log文件路径，获得返回回来的map进行数据库插入
	 */
	public String saveLogSum(){
		String path = "E:/object/fatal_2014-12-03-13.log";
		Integer userId = Integer.parseInt(loginUser.getUserId().toString());
		try {
			List<Map> list=readlog.BufferedReader(path);
			if(list.size()>0){
				for (Map map : list) {
					NspLogSum logsum=new NspLogSum();
					logsum.setServerId(1);
					logsum.setLogFileName(map.get("logFileName").toString());
					logsum.setLogTime(map.get("logTime").toString());
					logsum.setRequestType(map.get("requestType").toString());
					logsum.setResponseCode(map.get("responseCode").toString());
					logsum.setCount(Integer.parseInt(map.get("count").toString()));
					logsum.setStatus(query_status);
					logsum.setCreateUser(userId);
					nspLogSumService.saveNspLogSum(logsum);	
				}
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		message = "新增成功!";
		return RESULT_ADD;	
		
		
	}
	
	
	public void validateSave() {
	    	    	    	    	    	    	    if (nspLogSum.getLogFileName().length() == 0) {
			this.addActionError("** 日志文件 不能为空 !");
		}
			    	    	    if (nspLogSum.getLogTime().length() == 0) {
			this.addActionError("** 日志时间段 不能为空 !");
		}
			    	    	    if (nspLogSum.getRequestType().length() == 0) {
			this.addActionError("** 请求类型 不能为空 !");
		}
			    	    	    if (nspLogSum.getRequestContent().length() == 0) {
			this.addActionError("** 请求内容 不能为空 !");
		}
			    	    	    if (nspLogSum.getResponseContent().length() == 0) {
			this.addActionError("** 响应内容 不能为空 !");
		}
			    	    	    	    	    	    	    if (nspLogSum.getNote().length() == 0) {
			this.addActionError("** 备注 不能为空 !");
		}
			    	    	    if (nspLogSum.getResponseCode().length() == 0) {
			this.addActionError("** 响应代码 不能为空 !");
		}
			    	    	    	    	    if (nspLogSum.getStatus().length() == 0) {
			this.addActionError("** 状态 不能为空 !");
		}
			    	    
		this.setValidatorResult("/nsp/NspLogSumAdd.jsp");		
	}
	
	public String toEdit() {
	    Long id = new Long(selectedId);
	    
	    nspLogSum = nspLogSumService.getNspLogSum(id);
	
		return RESULT_EDIT;
	}
	
	public String update() {
	
	    nspLogSumService.updateNspLogSum(nspLogSum);	

		message = "修改成功!";
	    
		return RESULT_EDIT;
	}
	
	public void validateUpdate() {
					    			    			    if (nspLogSum.getLogFileName().length() == 0) {
			this.addActionError("** LogFileName 不能为空 !");
		}
			    			    if (nspLogSum.getLogTime().length() == 0) {
			this.addActionError("** LogTime 不能为空 !");
		}
			    			    if (nspLogSum.getRequestType().length() == 0) {
			this.addActionError("** RequestType 不能为空 !");
		}
			    			    if (nspLogSum.getRequestContent().length() == 0) {
			this.addActionError("** RequestContent 不能为空 !");
		}
			    			    if (nspLogSum.getResponseContent().length() == 0) {
			this.addActionError("** ResponseContent 不能为空 !");
		}
			    			    			    			    if (nspLogSum.getNote().length() == 0) {
			this.addActionError("** Note 不能为空 !");
		}
			    			    if (nspLogSum.getResponseCode().length() == 0) {
			this.addActionError("** ResponseCode 不能为空 !");
		}
			    			    			    if (nspLogSum.getStatus().length() == 0) {
			this.addActionError("** Status 不能为空 !");
		}
			    		this.setValidatorResult("/nsp/NspLogSumUpdate.jsp");	
	}
	
	public String delete() {	    
	    List ids = new ArrayList();
	    
		for(String id : selectedIds){
			ids.add(Long.valueOf(id));
		}
		
		try {
			this.nspLogSumService.delete(ids);
		} catch (Exception e) {
			System.out.println("异常....");

			this.addActionError("可能含有子记录约束冲突删除失败 !");
		}
		
		
		return queryByPage();
	}	
	
	

	/**
	 * getters and setters
	 * 
	 * @return
	 */
	 public void setNspLogSum(NspLogSum nspLogSum) {
		this.nspLogSum = nspLogSum;
	}
	
	public NspLogSum getNspLogSum() {
		return nspLogSum;
	}
	 
	 
	 public void setNspLogSumService(NspLogSumService nspLogSumService) {
		this.nspLogSumService = nspLogSumService;
	}
	
	public List<NspLogSum> getNspLogSumList() {
		return nspLogSumList;
	}

	public void setNspLogSumList(List<NspLogSum> nspLogSumList) {
		this.nspLogSumList = nspLogSumList;
	}
	
	public String getSelectedId() {
		return selectedId;
	}

	public void setSelectedId(String selectedId) {
		this.selectedId = selectedId;
	}

	public String[] getSelectedIds() {
		return selectedIds;
	}

	public void setSelectedIds(String[] selectedIds) {
		this.selectedIds = selectedIds;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}
	
	public List<Map> getResultList() {
		return resultList;
	}

	public void setResultList(List<Map> resultList) {
		this.resultList = resultList;
	}
	
	    public String getQuery_logSumId() {
        return this.query_logSumId;
    }
    
    public void setQuery_logSumId(String logSumId){
        this.query_logSumId = logSumId;
    }    

        public String getQuery_serverId() {
        return this.query_serverId;
    }
    
    public void setQuery_serverId(String serverId){
        this.query_serverId = serverId;
    }    

        public String getQuery_logFileName() {
        return this.query_logFileName;
    }
    
    public void setQuery_logFileName(String logFileName){
        this.query_logFileName = logFileName;
    }    

        public String getQuery_logTime() {
        return this.query_logTime;
    }
    
    public void setQuery_logTime(String logTime){
        this.query_logTime = logTime;
    }    

        public String getQuery_requestType() {
        return this.query_requestType;
    }
    
    public void setQuery_requestType(String requestType){
        this.query_requestType = requestType;
    }    

        public String getQuery_requestContent() {
        return this.query_requestContent;
    }
    
    public void setQuery_requestContent(String requestContent){
        this.query_requestContent = requestContent;
    }    

        public String getQuery_responseContent() {
        return this.query_responseContent;
    }
    
    public void setQuery_responseContent(String responseContent){
        this.query_responseContent = responseContent;
    }    

        public String getQuery_createUser() {
        return this.query_createUser;
    }
    
    public void setQuery_createUser(String createUser){
        this.query_createUser = createUser;
    }    

        public String getQuery_createTime() {
        return this.query_createTime;
    }
    
    public void setQuery_createTime(String createTime){
        this.query_createTime = createTime;
    }    

        public String getQuery_note() {
        return this.query_note;
    }
    
    public void setQuery_note(String note){
        this.query_note = note;
    }    

        public String getQuery_responseCode() {
        return this.query_responseCode;
    }
    
    public void setQuery_responseCode(String responseCode){
        this.query_responseCode = responseCode;
    }    

        public String getQuery_count() {
        return this.query_count;
    }
    
    public void setQuery_count(String count){
        this.query_count = count;
    }    

        public String getQuery_status() {
        return this.query_status;
    }
    
    public void setQuery_status(String status){
        this.query_status = status;
    }

	public NspLoggerService getNspLoggerService() {
		return nspLoggerService;
	}

	public void setNspLoggerService(NspLoggerService nspLoggerService) {
		this.nspLoggerService = nspLoggerService;
	}

	public Map getRequestTypeMap() {
		return requestTypeMap;
	}

	public void setRequestTypeMap(Map requestTypeMap) {
		this.requestTypeMap = requestTypeMap;
	}

	public NspLogSumService getNspLogSumService() {
		return nspLogSumService;
	}

	public String getQuery_requestTime() {
		return query_requestTime;
	}

	public void setQuery_requestTime(String query_requestTime) {
		this.query_requestTime = query_requestTime;
	}

	public String getQuery_requestTime_end() {
		return query_requestTime_end;
	}

	public void setQuery_requestTime_end(String query_requestTime_end) {
		this.query_requestTime_end = query_requestTime_end;
	}

	public Map getYearMap() {
		return yearMap;
	}

	public void setYearMap(Map yearMap) {
		this.yearMap = yearMap;
	}

	public Map getMonthMap() {
		return monthMap;
	}

	public void setMonthMap(Map monthMap) {
		this.monthMap = monthMap;
	}

	public String getQuery_yearStart() {
		return query_yearStart;
	}

	public void setQuery_yearStart(String query_yearStart) {
		this.query_yearStart = query_yearStart;
	}

	public String getQuery_monthStart() {
		return query_monthStart;
	}

	public void setQuery_monthStart(String query_monthStart) {
		this.query_monthStart = query_monthStart;
	}

	public String getQuery_yearEnd() {
		return query_yearEnd;
	}

	public void setQuery_yearEnd(String query_yearEnd) {
		this.query_yearEnd = query_yearEnd;
	}

	public String getQuery_monthEnd() {
		return query_monthEnd;
	}

	public void setQuery_monthEnd(String query_monthEnd) {
		this.query_monthEnd = query_monthEnd;
	}    

    
	
}
